/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.proteu.facesbeans;

/**
 *
 * @author eros
 */


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import javax.faces.event.ActionEvent;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.icesoft.faces.async.render.IntervalRenderer;
import com.icesoft.faces.async.render.RenderManager;
import com.icesoft.faces.async.render.Renderable;
import com.icesoft.faces.context.ViewListener;
import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
import com.icesoft.faces.webapp.xmlhttp.RenderingException;

public class IceClock implements Renderable, ViewListener{

	private static Log log = LogFactory.getLog(IceClock.class);

	private TimeZone serverTimeZone;
	private DateFormat serverFormat;

	private PersistentFacesState state;
	private IntervalRenderer clock;
	private final int renderInterval = 1000;

	public IceClock() {
        state = PersistentFacesState.getInstance();
        state.addViewListener(this);
        initTimeZone();
    }

    private void initTimeZone() {
    	serverTimeZone=TimeZone.getDefault();

//        SimpleDateFormat currentFormat = new SimpleDateFormat("EEE, HH:mm:ss");
        SimpleDateFormat currentFormat = new SimpleDateFormat("HH:mm");
        Calendar currentZoneCal = Calendar.getInstance(serverTimeZone);
        currentFormat.setCalendar(currentZoneCal);
        currentFormat.setTimeZone(serverTimeZone);
        serverFormat=currentFormat;
    }

    public String getServerTime() {
        return formatCurrentTime(serverFormat);
    }

    public static String formatCurrentTime(DateFormat dateFormat) {
        Calendar cal = dateFormat.getCalendar();
        cal.setTimeInMillis(System.currentTimeMillis());
        return dateFormat.format(cal.getTime());
    }

    public void setRenderManager(RenderManager renderManager) {
        clock = renderManager.getIntervalRenderer("clock");
        clock.setInterval(renderInterval);
        clock.add(this);
        clock.requestRender();
    }

    public RenderManager getRenderManager() {
        return null;
    }

    public void renderingException(RenderingException renderingException) {
        if (log.isDebugEnabled()) {
            log.debug("Rendering exception called because of " +
                      renderingException);
        }
        performCleanup();
    }

    protected boolean performCleanup(){
        try{
            if(clock != null){
                clock.requestStop();
                clock.remove(this);
                clock.dispose();
                clock = null;
            }
            return true;
        } catch (Exception failedCleanup){
            if (log.isErrorEnabled()) {
                log.error("Failed to cleanup a clock bean", failedCleanup);
            }

        }
        return false;
    }

	public PersistentFacesState getState() {
		return state;
	}

	public void viewCreated() {
		//empty implementation
	}

	public void viewDisposed() {
        if (log.isInfoEnabled()) {
            log.info("ViewListener of TestBean fired for a user - cleaning up");
        }
        performCleanup();
	}
}
